/*
 * sc_p_CO2.c
 *
 * Created: 2012-09-06 11:58:57
 *  Author: Antrykot
 */ 


#include "../sc-p/sc-p.h"
#include "../sc-p/utils.h"
#include "../sc-p/trans-p.h"
#include "../sc-p/measure.h"

u8 co2_mode = 0;
u8 can_get_aku = 0;
u8 conv_end = 0;
u8 can_transmit  = 0;

#define T1_START TCCR1B = (1 << 1);		// start, prescale 8,
#define T1_ENABLE TIMSK1 |= (1 << TOIE1);	// enable T1_OVF


u16 act_rms = 0;
u16 ref_rms = 0;
u8 rms_cnt = 0;

u16 v_ref = 0, v_act = 0;

int main(void)
{

	WAITMS(30);
	setup_io();
	init_eeprom();
	DCDC_ON;

	soft_start();
	WAITMS(300);
	
	
	T1_ENABLE;
	T1_START;
	
	sei();
	//TR_P_ENABLE;
	
	PTX_H;
	
	can_transmit = 1;
	
	while(1)
	{

		//TR_P_DISABLE;
		

		WAITMS(100);
		if (can_get_aku)
		{
			can_get_aku = 0;
			calc_aku();
			calc_pow();
			calc_dc();
			read_temp();
		}
		if (conv_end)
		{
			conv_end = 0;
			act_rms = act_rms / 4;
			ref_rms = ref_rms / 4;
			v_ref = ref_rms;
			v_act = act_rms;
			//t_pom = ref_rms - act_rms;
			//t_pom =  act_rms;
			calc_l(0);
			//t_pom = v_ref;
			act_rms = 0;
			ref_rms = 0;
		}
		
		//WAITMS(50);

	}
}




extern u8 add_params_num;
void interpret()
{
	switch(r_addr)
	{
		case P_ADDR_DCOFF:
		{
			DCDC_OFF;
			break;
		}
			
		case P_ADDR_INCAKU:
		{
			x_aku = read_word(EEP_X_AKU);
			x_aku += ADD_LESS;
			write_word(EEP_X_AKU,x_aku);
			add_params_num = 0;
			break;
		}
		case P_ADDR_DECAKU:
		{
			x_aku = read_word(EEP_X_AKU);
			x_aku -= ADD_LESS;
			write_word(EEP_X_AKU,x_aku);
			add_params_num = 0;
			break;
		}
		case P_ADDR_INCPOW:
		{
			x_pow = read_word(EEP_X_POW);
			x_pow += ADD_LESS;
			write_word(EEP_X_POW,x_pow);
			add_params_num = 1;
			break;
		}
		case P_ADDR_DECPOW:
		{
			x_pow = read_word(EEP_X_POW);
			x_pow -= ADD_LESS;
			write_word(EEP_X_POW,x_pow);
			add_params_num = 1;
			break;
		}
		case P_ADDR_INCAKUM:
		{
			x_aku = read_word(EEP_X_AKU);
			x_aku += ADD_MORE;
			write_word(EEP_X_AKU,x_aku);
			add_params_num = 0;
			break;
		}
		case P_ADDR_DECAKUM:
		{
			x_aku = read_word(EEP_X_AKU);
			x_aku -= ADD_MORE;
			write_word(EEP_X_AKU,x_aku);
			add_params_num = 0;
			break;
		}
		case P_ADDR_INCPOWM:
		{
			x_pow = read_word(EEP_X_POW);
			x_pow += ADD_MORE;
			write_word(EEP_X_POW,x_pow);
			add_params_num = 1;
			break;
		}
		case P_ADDR_DECPOWM:
		{
			x_pow = read_word(EEP_X_POW);
			x_pow -= ADD_MORE;
			write_word(EEP_X_POW,x_pow);
			add_params_num = 1;
			break;
		}
			
		case P_ADDR_L_FACTORY:
		{
			Zero = 0.92;
			Span = 0.29;
			T_cal = 300;
			//beta = 0.282;
			alpha = 0.000271;
			stn = 210;
		
			write_double(EEP_ZERO,Zero);
			write_double(EEP_SZERO,Zero);
			write_double(EEP_SPAN,Span);
			write_double(EEP_SSPAN,Span);
			//write_double(EEP_BETA,beta);
			//write_double(EEP_SBETA,beta);
			write_double(EEP_ALPH,alpha);
			write_double(EEP_SALPH,alpha);
			write_word(EEP_STN,stn);
		}

			
			
		case P_ADDR_L_RESET:
		{
			ser_def_low();
			break;
		}
			
		case P_ADDR_H_RESET:
		{
			ser_def_high();
			break;
		}
			
		case P_ADDR_SCALLZ:
		{
			cal_l_zero();
			
			break;
		}
		case P_ADDR_SCALLS:
		{
			cal_l_sens(r_data);
				
			write_double(EEP_ZERO,Zero);
			write_double(EEP_SPAN,Span);
			write_double(EEP_SZERO,Zero);
			write_double(EEP_SSPAN,Span);
			write_double(EEP_TCAL,T_cal);
			write_double(EEP_STCAL,T_cal);
			break;
		}

			
		case P_ADDR_CALLZ:
		{
			cal_l_zero();
				
			break;
		}
		case P_ADDR_CALLS:
		{
			cal_l_sens(r_data);
			write_double(EEP_ZERO,Zero);
			write_double(EEP_SPAN,Span);
			write_double(EEP_TCAL,T_cal);

			break;
		}

		case P_ADDR_RESET_DEFAULTS:
		{
			
			Zero = read_double(EEP_ZERO);
			Span = read_double(EEP_SPAN);
			T_cal = read_double(EEP_TCAL);
			alpha = read_double(EEP_ALPH);
			beta = read_double(EEP_BETA);		
			break;
		}			
		case P_ADDR_GETACT:
		{
			t_addr = P_ADDR_GETACT;
			t_data = debu_act;
			break;
		}
		case P_ADDR_GETREF:
		{
			t_addr = P_ADDR_GETREF;
			t_data = debu_ref;
			break;
		}
		case P_ADDR_GET_TCAL:
		{

			if (T_cal < 273)
			{
				t_data = (273 - T_cal) * 10;
				t_data |= (1 << 15);
			}
			else
			{
				t_data = (T_cal - 273) * 10;
			}
			t_addr = P_ADDR_GET_TCAL;
			break;
		}
		case P_ADDR_INCR_BETA:
		{
			beta += 0.005;
			t_data = beta * 1000;
			t_addr = P_ADDR_INCR_BETA;
			write_double(EEP_BETA,beta);
			break;
		}
		case P_ADDR_DECR_BETA:
		{
			beta -= 0.005;
			t_data = beta * 1000;
			t_addr = P_ADDR_DECR_BETA;
			write_double(EEP_BETA,beta);
			break;
		}
		case P_ADDR_GET_BETA:
		{
			t_data = beta * 1000;
			t_addr = P_ADDR_GET_BETA;

			break;
		}
 		case P_ADDR_SYNC:
 		{
	 		co2_mode = 0;
	 		CO2_HIGH;
	 		TCNT1 = 53035;	// 25ms
			DDRB |= (1 << 7);
			PORTB ^= (1 << 7);
			PORTB ^= (1 << 7);
			WAITUS(10);
			LR_OFF;
 		}

		
	}
		
		
}

